<?php
//127.0.0.1:2222/db.arpggame.com/auto/item_0.php?class=2&sub=0&page=1&max=10
header("Content-type: text/html; charset=utf-8");

require_once 'Snoopy-1.2.4/Snoopy.class.php';
require_once "mysql_connect.php"; //connect

//require_once '../include/water.php';//水印
function preg_substr($start, $end, $str) // 正则截取函数     
{     
    $temp = preg_split($start, $str);     
    $content = preg_split($end, $temp[1]);     
    return $content[0];     
}  
function str_substr($start, $end, $str) // 字符串截取函数     
{     
    $temp = explode($start, $str, 2);     
    $content = explode($end, $temp[1], 2);     
    return $content[0];     
} 
function strtr_words($str)//同义词替换
{
    $words=array();
    $content = file_get_contents('same.txt');//词库
    $content = str_replace( "\r", "",$content); //去掉换行符(以便兼容Linux主机)
    $content = preg_split('/\n/', $content, -1, PREG_SPLIT_NO_EMPTY);//\n分割字符
        foreach($content as $k=>$v)
        {
            if($k!=0)
            {
                $str_data = explode('=',$v);//关键词分割符
                $words+=array("$str_data[0]"=>"$str_data[1]");
            }
        }
    return strtr($str,$words);//返回结果
}

$i = $_GET['page'] ? $_GET['page'] : '1';
$class = $_GET['class'] ? $_GET['class'] : '0';
$sub = $_GET['sub'] ? $_GET['sub'] : '0';

$inv = $_GET['inv'] ? $_GET['inv'] : '0';

$n_url = $_GET['url'] ? $_GET['url'] : "http://www.battlenet.com.cn/wow/zh/item/?classId=$class&subClassId=$sub";

if ($inv != 0){
	$n_url = $n_url."&invType=$inv";
}

$max = $_GET['max'] ? $_GET['max'] : '10';


set_time_limit(0);


//for ($i = $min; $i <= $max; $i++) {

	
	$page = floor($i);
	$url = $n_url."&page=$page";      

	$snoopy = new Snoopy;   
	$snoopy->fetch($url); //获取所有内容    

	$snoopy->cookies["PHPSESSID"] = 'fc106b1918bd522cc863f36890e6fff7'; //伪装sessionid   
	$snoopy->agent = "(compatible; MSIE 4.01; MSN 2.5; AOL 4.0; Windows 98)"; //伪装浏览器   
	$snoopy->referer = "http://www.google.com"; //伪装来源页地址 http_referer   
	$snoopy->rawheaders["Pragma"] = "no-cache"; //cache 的http头信息   
	$snoopy->rawheaders["X_FORWARDED_FOR"] = "127.0.0.1"; //伪装ip   
	//$snoopy->submit($action,$formvars);   
	$snoopy->expandlinks = true; //是否补全链接

	$results= $snoopy->results;//输出全文
	//$results = iconv("gb2312","UTF-8",$results);
	//echo $snoopy->fetchtext; //获取文本内容（去掉html代码）   
	//echo $snoopy->fetchlinks; //获取链接   
	//echo $snoopy->fetchform ; //获取表单 


	//$results = str_substr('<table cellspacing="0" cellpadding="0" summary="forum_7">', '</table>', $results);

	$results = str_substr('<div class="table full-width">', '<div class="table-options data-options ">', $results);

	/*
	$results = ereg_replace('class="row1"','class="row"',$results);
	$results = ereg_replace('class="row2"','class="row"',$results);

	echo $results;*/
	//输出列表页链接
	preg_match_all('/href="\/wow\/zh\/item\/(.+?)"/',$results,$page);


	foreach ($page[1] as $k => $outpage) {
		//$outpage = iconv("gb2312","UTF-8",$outpage);
		
		$item_id = $outpage;
		
		$out_url = "http://www.battlenet.com.cn/wow/zh/item/".$outpage;
		


		if (ceil($i) == $i){
			$point = $k > 24;
		}else{
			$point = $k < 25;
		}
		
		

		if ($point){
			echo "已跳出";
		}else{

		

		
		if (!empty($out_url)){
		
			
			//内容页采集
			//$outpage="http://bbs.duowan.com/thread-26675724-1-1.html";  
			$snoopy->fetch($out_url); //获取所有内容    
			$results = $snoopy->results;//全文
			//$results = iconv("gb2312","UTF-8",$results);//GBK OR U8
			

			$name = str_substr('<div class="title">', '</div>', $results);
			
			$item_color = str_substr('color-', '"', $name); //品质
			$name = trim(strip_tags($name)); // 物品名称
			

			
			$icon  = str_substr('<div class="item-detail">', '</div>', $results);
			$icon  = str_substr('background-image: url("','");',$icon);
			$specs = trim(str_substr('<ul class="item-specs" style="margin: 0">', '</ul>', $results));
			

					
					//SAVE ICON
					$icon_url 	 = trim(str_ireplace("http://content.battlenet.com.cn/", '', $icon));
					$now_name 	 = "/".basename($icon);
					$now_dir	 = trim(str_ireplace($now_name, '', $icon_url));

					$dir_name 	 = "../uploads/".$now_dir; 
					if(!file_exists($dir_name)){
						mkdir($dir_name, 0777, true);
						@chmod($dir_name, 511);//0777 八进制
					}
					
					$icon_url= $now_dir."/".(basename($icon));
					/*
					if(!file_exists($dir_name."/".(basename($icon)))){
						file_put_contents($dir_name."/".(basename($icon)), file_get_contents($icon));
					}*/
			
			
			$viewer = str_substr('<div class="loading">', '</div>', $results);
			$viewer = str_substr('background-image: url(\'', '\');', $viewer);
			
					//SAVE viewer
					$viewer_url 	 = trim(str_ireplace("http://content.battlenet.com.cn/", '', $viewer));
					$now_name 	 = "/".basename($viewer);
					$now_dir	 = trim(str_ireplace($now_name, '', $viewer_url));

					$dir_name 	 = "../uploads/".$now_dir; 
					if(!file_exists($dir_name)){
						mkdir($dir_name, 0777, true);
						@chmod($dir_name, 511);//0777 八进制
					}
					
					$viewer_url= $now_dir."/".(basename($viewer));
					/*
					if(!file_exists($dir_name."/".(basename($viewer)))){
						file_put_contents($dir_name."/".(basename($viewer)), file_get_contents($viewer));
					}*/

			

			 
		
			/*
			$allmain = preg_replace("'<script[^>]*?>.*?</script>'si", "", $allmain);
			//$allmain = ereg_replace("<div [^>]*>","",$allmain);//去除DIV
			//$allmain = ereg_replace("<\/div>","",$allmain);//去除DIV
			$allmain = ereg_replace("<A [^>]*>|<\/A>","",$allmain);//去除超链接 大A
			$allmain = ereg_replace("<a [^>]*>|<\/a>","",$allmain);//去除超链接 小a
			$allmain = ereg_replace("<iframe [^>]*>|<\/iframe>","",$allmain);//去IFRAME
			
			*/

	
			
			if ($name == "" ){
				
				echo "标题为空";
			}else{
				//入库
				
				mysql_query('set names utf8');  // select u8
				$cf_result=mysql_query("select * from db_item WHERE item_id='$item_id'");//重复性
				if(mysql_num_rows($cf_result)){
					echo $name.'——重复文章跳过。。。<br/>';	
				}else {
				
					if ($specs==""){
						echo "内容为空";
					}else{

						$sql = "INSERT INTO db_item (item_id, name, icon, viewer, item_color, specs, class, sub, inv)
						VALUES ('$item_id','$name','$icon_url','$viewer_url','$item_color','$specs','$class','$sub','$inv')";// insert
						mysql_query('set names utf8');  // select u8
						
						if(mysql_query($sql)){
							
							echo ($name.'——已成功发布'."<br/>"); //if mysql error

						}else{
							echo '连接出错POST表，未发布。跳过'.mysql_error()."<br/>";
						}
					
						}
					}
			}
			
			
	}
	}
	}
//}

$i = $i + 0.5;

if ($i > ($max + 0.5)){
	echo "<br/><font color:'red'>全部完成！$i</font>";
	echo "<script>alert('全部完成！')</script>";
}else{
	echo "<script>location.href='item_0.php?page=$i&max=$max&class=$class&sub=$sub&inv=$inv'</script>";
}
?>